<?php
namespace Foxframe;

use Symfony\Component\Yaml\Yaml;

class Menu
{
	public $html;

	public function __construct($file)
	{
		if (is_file($file))
		{
			$menu	= Yaml::parse($file);
			$this->render($menu['menu']);
		}
	}

	public function render($menu)
	{
		$html	= "<ul>";
		foreach ($menu as $item)
		{
			if (isset($item['submenu']) && count(isset($item['submenu'])))
				$class = 'parent';
			else
				$class = null;

			$dataTarget	= null;
			$dataToggle	= null;

			if (isset($item['data-target']))
				$dataTarget = "data-target='{$item['data-target']}'";
			if (isset($item['data-toggle']))
				$dataToggle = "data-toggle='{$item['data-toggle']}'";

			if (isset($item['href']) && strpos($item['href'],'http') == false)
				$href = BASE_PATH.$item['href'];
			else if (isset($item['href']))
				$href = $item['href'];
			else
				$href = '';

			if (isset($item['id']))
				$id = $item['id'];
			else
				$id = 'menu_'.str_replace('/', '_', $href);

			if (strtolower($id) == 'menu_'.str_replace('/', '_', strtolower(\Foxframe\Uri::getInstance()->getUrl())))
				$class.= ' current_page_item';

			$html.= "<li class='{$class}' id='{$id}'>";
				if ($class == 'parent')
					$html.= "<a href='{$href}' class='ico-down-circle icoPosRight'>";
				else
					$html.= "<a href='{$href}' {$dataTarget} {$dataToggle}>";
					if (isset($item['class']))
						$html.= "<i class='{$item['class']}'></i> ";
					$html.= $item['name'];
				$html.= "</a>";
				if (isset($item['submenu']) && count(isset($item['submenu'])))
					$html.= $this->render($item['submenu']);
			$html.= "</li>";
		}
		$html.= "</ul>";
		$this->html	= $html;

		return $html;
	}
}